<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>编辑文章</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <!-- jq -->
    <script src="./libs/jquery/jquery.min.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            编辑文章
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputTitle" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="text" name="title" class="form-control title" id="inputTitle" value="文章标题文字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCover" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img id="cimg" src="./images/2.jpg" class="article_cover">
                        <input name="cover" type="file" id="inputCover" onchange="changeCoverImage()">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCategory" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select id="category" class="form-control category" name="categoryId">

                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">
                        <input id="articleDate" name='date' class="jeinput" type="text" placeholder="请选择发布日期">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <!-- 富文本编辑区域 -->
                        <textarea id="aContent">请输入文章内容</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="button" class="btn btn-success btn-edit" onclick="saveArticle('已发布')">修改</button>
                        <button type="button" class="btn btn-default btn-draft" onclick="saveArticle('')">存为草稿</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script src="./libs/art-template/template-web.js"></script>
    <script src="./js/jqconfig.js"></script>
    <script src="./libs/layui/layui.js"></script>
    <script src="./libs/tinymce_5.5.1/tinymce.min.js"></script>
    <script id="catelist" type="text/html">
    {{each data}}
        <option value="{{$value.id}}">{{$value.name}}</option>
    {{/each}}
    </script>
    <script>
        let params_id = location.href.split('=')[1];
        layui.use('laydate', function () {
            let laydate = layui.laydate;

            // 初始化日期控件
            laydate.render({
                elem: '#articleDate' //指定元素
                , type: 'datetime'
            });

            getCateList();
            initTiny(); // 渲染富文本编辑器

        })
        // $(function () {
        //     // location.href 会有一个参数查询字符串: ?id=221 表示当前文章的id
        //     // alert(location.href);
        //     getCateList();
        // })

        // 1.0 初始化页面的相关元素
        function getCateList() {
            $.ajax({
                url: '/admin/category/list',
                type: 'get',
                success: res => {
                    $('#category').html(template('catelist', res))
                }
            })
        }

        // 1.0.1 进行富文本编辑器的初始化
        function initTiny() {
            tinymce.init({
                selector: '#aContent',
                // 汉化
                language: 'zh_CN',
                // 设定富文本编辑器渲染完毕之后的回调函数
                setup: editor => {
                    editor.on('init', () => {
                        // 当富文本编辑器渲染完毕之后的回调
                        // alert('富文本编辑器渲染完毕')
                        getArticle(); // 获取数据给富文本编辑器设置
                    })
                }
            });
        }


        // 2.0 定义方法用来根据id获取到文章的老数据进行回显
        function getArticle() {
            // 2.0.1 获取id location.href=> xxxx/.html?id=229
            // let id = location.href.split('=')[1];
            let id = params_id;
            // alert(id);
            // 2.0.2 请求服务器接口获取文章老数据
            $.ajax({
                url: '/admin/article/search',
                type: 'get',
                data: { id },
                success: res => {
                    let { title, cover, categoryId, date, content } = res.data;
                    // 2.0.2 数据回显到页面
                    $('#inputTitle').val(title)
                    $('#cimg').prop('src', cover)
                    $('#category').val(categoryId)
                    $('#articleDate').val(date)
                    // 给富文本编辑器设置值
                    tinyMCE.editors['aContent'].setContent(content)
                }
            })
        }

        // 3.0 saveArticle方法;负责文章的更新
        function saveArticle(state) {
            // 1.0 构造数据 fromData
            let id = params_id;
            let title = $('#inputTitle').val();
            let category = $('#category').val();
            let articleDate = $('#articleDate').val();
            let content = tinyMCE.editors['aContent'].getContent();
            let cover = document.querySelector('#inputCover').files[0];

            let formData = new FormData();
            formData.append('id', id);
            formData.append('title', title);
            formData.append('cover', cover);
            formData.append('categoryId', category);
            formData.append('date', articleDate);
            formData.append('content', content);
            formData.append('state', state);


            // 2.0 ajax请求实现文章编辑
            $.ajax({
                url: '/admin/article/edit',
                type: 'post',
                data: formData,
                contentType: false,
                processData: false,
                success: res => {
                    // 3.0 跳转回文章列表页面
                    location.href = './article_list.html';
                }
            })
        }

    </script>
</body>

</html>